//
//  HXIflyPersonalDataTableViewController.m
//  KD
//
//  Created by GaoFei on 16/7/8.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXIflyPersonalDataTableViewController.h"

#import "NSString+Frame.h"

#import "HXIflyPersonalDataTableHeaderView.h"//头视图
#import "HXIflyJobRecordTableViewCell.h"//单元格
#import "HXIflyJobRecordDetailsTableViewController.h"//日志详情
#import "HXGetMembersDayWorkModel.h"

@interface HXIflyPersonalDataTableViewController ()

//@property (nonatomic,copy) NSString * personName;
//@property (nonatomic,copy) NSString * membersId;
//@property (nonatomic,copy) NSString * phoneNum;
//@property (nonatomic,copy) NSString * groupName;

@property (nonatomic,strong) HXGetGroupMembersListModel * membersModel;
@property (nonatomic,copy) NSString * groupsId;
@property (nonatomic,copy) NSString * userId;
@property (nonatomic,strong) HXIflyPersonalDataTableHeaderView * headerView;
@property (nonatomic,strong) NSMutableArray <HXGetMembersDayWorkListModel *> * dataArr;

@property (nonatomic,assign) NSInteger pageNum;

@end

@implementation HXIflyPersonalDataTableViewController

#pragma mark ----// 懒加载 \\----
-(NSMutableArray<HXGetMembersDayWorkListModel *> *)dataArr{

    if (_dataArr == nil) {
        
        _dataArr = [[NSMutableArray alloc] init];
    }
    
    return _dataArr;
}

/**
 重写init方法 1.需要用户名 2.
 */
-(instancetype)initWithGroupMembersModel:(HXGetGroupMembersListModel *)membersModel withGroupsId:(NSString *)groupsId{

    self = [super init];
    
    if (self) {
        
        self.membersModel = membersModel;
        self.groupsId = groupsId;
//        self.personName = membersModel.PersonName;
//        self.membersId = membersModel.MemberID;
    }
    
    return self;
}

/**
 重写init方法 1.需要用户名 2.用户id
 */
-(instancetype)initWithUserID:(NSString *)userId
                 withGroupsId:(NSString *)groupsId{

    self = [super init];
    
    if (self) {
        
        self.userId   = userId;
        self.groupsId = groupsId;
        //        self.personName = membersModel.PersonName;
        //        self.membersId = membersModel.MemberID;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.membersModel.PersonName;
    
    if (_headerView == nil) {
        
        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        
        _headerView = [[[NSBundle mainBundle] loadNibNamed:@"HXIflyPersonalDataTableHeaderView" owner:nil options:nil] objectAtIndex:0];
        self.headerView.frame = CGRectMake(0, 0, HXSCREEN_WIDTH, 200);
        self.tableView.tableHeaderView = _headerView;
    }
    
    //加载数据
    [self loadCreateData];
}

/**
 加载数据
 */
-(void)loadCreateData{

    [SVProgressHUD showWithStatus:@"正在加载..."];
    
    [NetworkAPI getGroupsMembersWithGroupId:self.groupsId withMembersId:self.userId Success:^(HXGetGroupMembersModel *result) {
        
        if ([result.code isEqualToString:@"200"]) {
            
            self.membersModel = result.result[0];
            self.headerView.membersModel = self.membersModel;
        }
        
    } failure:^(NSError *err) {
        
    }];
    
    [self loadCreateDataMethod];
}

/**
 加载单元格的数据
 */
-(void)loadCreateTableViewData{
    
    self.pageNum = 0;

    [SVProgressHUD showWithStatus:@"正在加载..."];
    [NetworkAPI getDayWorkListWithGroupId:self.groupsId
                            withMembersId:self.userId
                             withPageSize:@"10"
                              withPageNum:[NSString stringWithFormat:@"%ld",(long)self.pageNum]
                                  Success:^(HXGetMembersDayWorkModel *result) {
                                      if ([result.code isEqualToString:@"200"]) {
                                          
                                          [self.tableView.mj_footer resetNoMoreData];
                                          [SVProgressHUD showSuccessWithStatus:@"刷新成功"];
                                          [self.tableView.mj_header endRefreshing];
                                          
                                          self.pageNum ++;
                                          [self.dataArr removeAllObjects];
                                          self.dataArr = [result.result mutableCopy];
                                          [self.tableView reloadData];
                                          
                                          if ([result.result count] < 10) {
                                              [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                              self.tableView.mj_footer.alpha = 1.0f;
                                              return;
                                          }

                                      }
                                      else if([result.code isEqualToString:@"400"]){
                                          [SVProgressHUD showErrorWithStatus:@"暂无数据"];
                                          [self.tableView.mj_header endRefreshing];
                                          [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                          self.tableView.mj_footer.alpha = 1.0f;
                                      }else{
                                          [SVProgressHUD showErrorWithStatus:@"暂无更多"];
                                          [self.tableView.mj_header endRefreshing];
                                      }

                                  }
                                  failure:^(NSError *err) {
                                      [SVProgressHUD showErrorWithStatus:@"网络错误!"];
                                  }];
}

-(void)getMoreTableViewData {
    
    [NetworkAPI getDayWorkListWithGroupId:self.groupsId
                            withMembersId:self.userId
                             withPageSize:@"10"
                              withPageNum:[NSString stringWithFormat:@"%ld",(long)self.pageNum]
                                  Success:^(HXGetMembersDayWorkModel *result) {
                                      [SVProgressHUD dismiss];
                                      if ([result.code isEqualToString:@"200"]) {
                                          
                                          if ([result.result count] == 0) {
                                              [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                              self.tableView.mj_footer.alpha = 1.0f;
                                              return;
                                          }else if ([result.result count] < 10) {
                                              [self.dataArr addObjectsFromArray:result.result];
                                              [self.tableView reloadData];
                                              [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                              self.tableView.mj_footer.alpha = 1.0f;
                                          }else {
                                              self.pageNum++;
                                              [self.dataArr addObjectsFromArray:result.result];
                                              [self.tableView reloadData];
                                              [self.tableView.mj_footer endRefreshing];
                                              self.tableView.mj_footer.alpha = 0;
                                          }
                                          
                                      }
                                      else if([result.code isEqualToString:@"400"]){
                                          [SVProgressHUD showErrorWithStatus:@"暂无数据"];
                                          [self.tableView.mj_header endRefreshing];
                                          [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                          self.tableView.mj_footer.alpha = 1.0f;
                                      }else{
                                          [SVProgressHUD showErrorWithStatus:@"暂无更多"];
                                          [self.tableView.mj_footer endRefreshing];
                                          self.tableView.mj_footer.alpha = 0;
                                      }
                                      
                                  }
                                  failure:^(NSError *err) {
                                      [SVProgressHUD showErrorWithStatus:@"网络错误!"];
                                  }];

}

/**
 加载加载的方法
 */
-(void)loadCreateDataMethod{
    
    self.tableView.mj_header=[AWRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadCreateTableViewData)];
    self.tableView.mj_footer=[AWRefreshFooder footerWithRefreshingTarget:self refreshingAction:@selector(getMoreTableViewData)];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    self.tableView.mj_footer.automaticallyHidden = NO;
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
    return 1;
}

/**
 返回UITableView的头视图
 */
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 20.f;
    }
    else {
        return 4.f;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXIflyJobRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXIflyJobRecordTableViewCell"];
    
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HXIflyJobRecordTableViewCell" owner:nil options:nil] objectAtIndex:0];
    }
    
    cell.isLastViewDetail = YES;
    HXGetMembersDayWorkListModel * model = self.dataArr[indexPath.section];
    cell.dayWorkModel = model;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    CGFloat height = 138.f-17.f;
    
    HXGetMembersDayWorkListModel * model = self.dataArr[indexPath.section];
    
    if (model.Content.length > 0) {
        
        height = height + [model.Content heightWithFont:[UIFont systemFontOfSize:16] withinWidth:HXSCREEN_WIDTH-3*8];
    }
    
    
    return height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    HXGetMembersDayWorkListModel * model = self.dataArr[indexPath.section];
    
    HXIflyJobRecordDetailsTableViewController * jobVc = [[HXIflyJobRecordDetailsTableViewController alloc] initWithMessageID:model.MessageID withSendUserID:model.SendUserID];
    
    jobVc.personSupVC = self;
    jobVc.jiemianID = 2;
    
    [self.navigationController pushViewController:jobVc animated:YES];
}

@end
